package com.lzl.mall.order.controller;

import com.lzl.mall.order.entity.R;
import com.lzl.mall.order.entity.UserVO;
import com.lzl.mall.order.entity.Wallet;
import com.lzl.mall.order.service.DetailService;
import com.lzl.mall.order.service.WalletService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/order")
public class OrderController {
     @Autowired
     private WalletService walletService;

     @Autowired
     private DetailService detailService;
    /**
     * @description 1.  查询用户钱包余额
     * @createTime 2023/2/27 13:13
     * @param
     * @return com.lzl.mall.order.entity.R
     */
    @GetMapping("/query")
    public R query(@Param("username") String username){
        Wallet wallet = walletService.query(username);
        if(wallet!=null){
            return R.ok().put(username,wallet);
        }
        return R.error().put(username,"钱包用户中未找到该用户");
    }

    /**
     * @description 2. 用户消费100元的接口
     * @createTime 2023/2/27 13:14
     * @param
     * @return com.lzl.mall.order.entity.R
     */
    @PostMapping("/consume")
    public R consume(@RequestBody UserVO userVO){
        R consume = walletService.consume(userVO);
        int  code = (int)consume.get("code");
        if(code == 0){
            return R.ok("success");
        }
        return R.error("消费失败");
    }

    /**
     * @description 3. 用户退款20元接口
     * @createTime 2023/2/27 13:14
     * @param
     * @return com.lzl.mall.order.entity.R
     */
    @PostMapping("/refund")
    public R refund(@RequestBody UserVO userVO){
        R consume = walletService.refund(userVO);
        int  code = (int)consume.get("code");
        if(code == 0){
            return R.ok("success");
        }
        return R.error("退款失败");
    }

    /**
     * @description 4. 查询用户钱包金额变动明细的接口
     * @createTime 2023/2/27 13:14
     * @param
     * @return com.lzl.mall.order.entity.R
     */
    @GetMapping("/detail")
    public R queryDetail(@Param("username") String username){

        List list = detailService.queryDetail(username);
        if(list!=null){
            return R.ok().put(username,list);
        }
        return R.error().put(username,"钱包用户中未找到该用户");
    }

}
